<?php
declare(strict_types=1);
/**
 * Created by PhpStorm
 * User: Administrator
 * Date: 2023/7/17
 * Time: 14:48:29
 */

namespace Service\GoodsService;

use App\Model\BbcModel\Goods;
use Hyperf\DbConnection\Db;

class GoodsLinkService extends \Service\BaseService
{
    /**
     * 获取店铺商品
     * @param $mn_user
     * @param array $goodsIds
     * @return mixed
     */
    public function getGoodsMnLinkList($mn_user,array $goodsIds)
    {
        return Goods::instance()
            ->leftJoin('ly_mn_store_goods',"lg.id", "=", "ly_mn_store_goods.goods_id")
            ->leftJoin('ly_goods_batch',"ly_goods_batch.goods_id", "=", "ly_goods.id AND ly_goods.is_trade = ly_goods_batch.type")
            ->whereIn('ly_goods.id',$goodsIds)
            ->where('ly_mn_store_goods.mn_sid',$mn_user['id'])
            ->where(function ($query) use ($mn_user){
                if($mn_user['type'] == 2){
                    $query->select(Db::raw("((ly_goods.status=1 and ly_goods.df=0 and ly_goods.is_trade=0) or (ly_goods.is_trade=1 and (ly_mn_store_goods.df=0 or (ly_mn_store_goods.df=1 and ly_mn_store_goods.num>0) or (ly_mn_store_goods.pre_sell=1 and ly_mn_store_goods.pre_storage>0))))"));
                }else{
                    $query->select(Db::raw("lg.is_trade=0"));
                }
            })
            ->where(function ($query) use ($mn_user){
                if($mn_user['type']==3){
                    switch ($mn_user['goods_source']){
                        case 1://只卖普通仓
                            $query->where(Db::raw("(ly_goods.is_fast_delivery=0)"));
                            break;
                        case 2://只卖前置仓
                            $query->where(Db::raw("(ly_goods.supplier_id=$mn_user[supplier_id])"));
                            break;
                        case 3://前置仓+普通仓
                            $query->where(Db::raw("((ly_goods.supplier_id=$mn_user[supplier_id]) or (ly_goods.is_fast_delivery=0))"));
                            break;
                        case 4://只卖中心仓
                            $query->where(Db::raw("(ly_goods.is_fast_delivery=2)"));
                            break;
                        case 5://中心仓+普通仓
                            $query->where(Db::raw("(ly_goods.is_fast_delivery<>1)"));
                            break;
                        case 6://中心仓+前置仓
                            $query->where(Db::raw("((ly_goods.supplier_id=$mn_user[supplier_id]) or (ly_goods.is_fast_delivery=2))"));
                            break;
                        case 7://所有仓
                            $query->where(Db::raw("((ly_goods.supplier_id=$mn_user[supplier_id]) or (ly_goods.is_fast_delivery in (0,2)))"));
                            break;
                    }
                }else{
                    $query->where(Db::raw("(lg.is_fast_delivery <> 1)"));
                }
            })
            ->select([
                "ly_goods.is_trade",
                "ly_goods.id",
                "ly_mn_store_goods.mn_sid",
                "ly_goods_batch.batch_no",
                "ly_goods_batch.spec_list"
            ])
            ->get();
    }
}